package graphics;

import java.util.Hashtable;

import model.ChessGame;
import model.Piece;
import model.Square;
import graphics.interfaces.IGameView;
import graphics.view2D.Board2D;
import graphics.view2D.Coord2D;
import graphics.view2D.MouseProcessor2D;
import graphics.view2D.Piece2D;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import controller.GameController;

public class Board2DView implements IGameView {
	
	//private GameController gameController;
	private ChessGame chessGame;
	private MouseProcessor2D mouseProcessor;		
	private SpriteBatch batch;
	
	private Board2D board2D;
	private Hashtable<Piece, Piece2D> pieces2D;
	
	public Board2DView() {
		batch = new SpriteBatch();
		board2D = new Board2D();
		pieces2D = new Hashtable<Piece, Piece2D>();
	}
		
	
	public void Load(GameController gameController, ChessGame chessGame){
		//this.gameController = gameController;
		this.chessGame = chessGame;
		this.board2D.LoadContent();

		String chessSet2D = "set2D.png";
		
		// Creation of the full sprite set
		for(int i = 0;i<12;i++){
			pieces2D.put(Piece.values()[i], new Piece2D(Piece.values()[i], chessSet2D));
		}
		// Mouse imput manager
		mouseProcessor = new MouseProcessor2D(gameController);
		Gdx.input.setInputProcessor(mouseProcessor);
	}
	
	public void Draw(){
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		batch.begin();
		board2D.Draw(batch);
		for (int i = 0; i < 64; i++) {
			Piece pType = this.chessGame.getPiece(i);
			
			if(pType != Piece.EMPTY){
				int x = Coord2D.getInstance().getXFromSquare(Square.get(i));
				int y = Coord2D.getInstance().getYFromSquare(Square.get(i));
				this.pieces2D.get(pType).SetPosition(x, y);
				this.pieces2D.get(pType).Draw(batch);
			}
		}
		batch.end();
	}

	public void SelectSquare(Square square) {
		board2D.SelectSquare(square);		
	}

	public void UnselectSquare() {
		board2D.UnselectSquare();
	}
	
	
}
